<template>
  <div class="newslist">
    <div class="news-line font-xbs"></div>
    <img
      src="/assets/imgs/arrow.png"
      class="news-arrow unselectable"
      alt=""
      style="height: 20px"
    />
    <div class="card-section">
      <div class="card-row" v-for="(item, row) in rownum" :key="item">
        <Newscard
          v-for="item2 in articleRowNum(row)"
          :key="item2"
          :article-id="user.fav.list[(item - 1) * 3 + item2 - 1]"
        />
      </div>
    </div>
    <div
      class="news-more font-xbs"
      @click="showMore()"
      v-if="user.fav.total - rownum * 3 > 0"
    >
      展开更多
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, onBeforeMount } from 'vue';
import Newscard from './Newscard.vue';
import { userinfo } from '@/stores/counter';
import articleStar from '@/api/articleStar';
const user = userinfo();
const { getFavArticle } = articleStar();
const rownum = computed(() => {
  console.log(Math.min(user.fav.list.length, user.fav.total));
  return Math.ceil(Math.min(user.fav.list.length, user.fav.total) / 3);
});

onMounted(() => {
  console.log('子组件开始mounted');
});

function showMore() {
  user.fav.page++;
  getFavArticle();
}

const articleRowNum = (row: number) => {
  console.log('子组件准备for了');
  console.log(user.fav.list.length);
  if (user.fav.list.length == 0) {
    return 0;
  }
  return Math.min(3, user.fav.list.length - row * 3);
};
</script>

<style scoped>
.news-arrow {
  margin-top: 30px;
}

.news-more {
  margin-top: 30px;
  color: #f59009;
  font-size: 24px;
  cursor: pointer;
}

.news-line {
  margin-top: 20px;
  height: 1px;
  background-color: #fbdcbe;
  width: 50%;
}

.newslist {
  padding: 30px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
}

.card-section {
  width: 100%;
  max-width: 1920px;
  height: max-content;
  /* background-color: aqua; */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.card-row {
  width: 100%;
  height: max-content;
  /* background-color: aqua; */
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
}
</style>
